﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace SilverFlow.Controls
{
    /// <summary>
    /// Thumbnail of the minimized windows.
    /// </summary>
    [TemplatePart(Name = PART_Title, Type = typeof(TextBlock))]
    [TemplatePart(Name = PART_Thumbnail, Type = typeof(Image))]
    public class WindowIcon : Button
    {
        // Template parts
        private const string PART_Title = "Title";
        private const string PART_Thumbnail = "Thumbnail";

        /// <summary>
        /// Gets or sets the window title that is displayed on the icon />.
        /// </summary>
        /// <value>The title displayed on the icon.</value>
        public string Title { get; set; }

        /// <summary>
        /// Gets or sets the thumbnail.
        /// </summary>
        /// <value>The thumbnail.</value>
        public ImageSource Thumbnail { get; set; }

        /// <summary>
        /// Gets or sets the FloatingWindow associated with the icon.
        /// </summary>
        /// <value>Floating window.</value>
        public FloatingWindow Window { get; set; }

        /// <summary>
        /// Window's title.
        /// </summary>
        private TextBlock title;

        /// <summary>
        /// Thumbnail icon of the window.
        /// </summary>
        private Image thumbnail;

        /// <summary>
        /// Builds the visual tree for the <see cref="WindowIcon" /> control 
        /// when a new template is applied.
        /// </summary>
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            title = GetTemplateChild(PART_Title) as TextBlock;
            thumbnail = GetTemplateChild(PART_Thumbnail) as Image;

            if (title == null)
                throw new NotImplementedException("Template Part PART_Title is required to display WindowIcon.");

            if (thumbnail == null)
                throw new NotImplementedException("Template Part PART_Thumbnail is required to display WindowIcon.");

            title.Text = Title;
            thumbnail.Source = Thumbnail;
        }
    }
}
